package top.leyi.handle;

import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import top.leyi.IService.IPushService;
import top.leyi.entity.Talk.Talk;
import top.leyi.entity.pushAndColle.Push;
import top.leyi.entity.user.User;
import top.leyi.exception.TalkNotFoundException;
import top.leyi.utils.Result;
import top.leyi.utils.ResultGenerator;

import java.util.concurrent.ConcurrentHashMap;

/**
 * @desc 推送消息
 */

@RestController
@RequestMapping("/api/push")
public class PushHandle {
    @Autowired
    IPushService pushService;
    @GetMapping("")
    public Result doGet(  @RequestParam("id") int userId,
                          @RequestParam("token") String token) {
        User user = new User();
        user.setId(userId);


        return new Result(200, pushService.getAllPush(user));
    }

    @GetMapping("/system")
    public Result doGetSys(@RequestParam("id") int id,
                           @RequestParam("token") String token){


        return new Result(200, pushService.getSysPush(id));
    }

    @DeleteMapping("")
    public Result doDelete(@RequestParam("pushId") int pushId,
                           @RequestParam("token") String token ,
                           @RequestParam("id") int userId) {
        Push push = new Push();
        push.setId(pushId);

        User user = new User();
        user.setToken(token);
        user.setId(userId);

        pushService.removePush(push, user);
        return ResultGenerator.OK.getResult();
    }

    /**
     * @desc 系统管理员添加push
     */
    @PostMapping("")
    public Result doPost(@RequestParam("id") int userId,
                         @RequestParam("token") String token,
                         @RequestParam("content") String content,
                         @RequestParam(value = "pics", required = false) String pics
                         ) throws TalkNotFoundException {
        Talk talk = new Talk();
        talk.setContent(content);
        talk.setPics(pics);
        talk.setUser_id(userId);

        User user = new User();
        user.setId(userId);
        user.setToken(token);


        pushService.addOnePush(talk );
        return ResultGenerator.OK.getResult();
    }

}
